<?php

function is_score_valid($a, $b) {
	$a = intval ( $a );
	$b = intval ( $b );
	
	if ($a == $b) {
		return false;
	}
	if ($a < $b) {
		return is_score_valid ( $b, $a );
	}
	if ($a == $b + 1 || $a < 11 || ($a > 11 && $a != $b + 2)) {
		return false;
	}
	return true;
}

function get_single_match_result($coef, $a, $b, &$sa, &$sb) {
	$coef = doubleval ( $coef );
	$a = intval ( $a );
	$b = intval ( $b );
	$sa = intval ( $sa );
	$sb = intval ( $sb );
	
	// dump ( $coef );
	// dump ( $a );
	// dump ( $b );
	// dump ( $sa );
	// dump ( $sb );
	
	if ($a == $b) {
		return - 1;
	}
	if ($a < $b) {
		return get_single_match_result ( $coef, $b, $a, $sb, $sa );
	}
	if ($a == $b + 1 || $a < 11 || ($a > 11 && $a != $b + 2)) {
		return - 1;
	}
	
	$pa = 10;
	$pb = $b > 10 ? 10 : $b;
	
	$inc_a = $sb * 0.05 + $pa * $sb * 0.01 - $pb * $sa * 0.01 + 30;
	$inc_b = - $sb * 0.05 + $pb * $sa * 0.01 - $pa * $sb * 0.01 + 10;
	
	$sa += intval ( round ( $inc_a * $coef ) );
	$sb += intval ( round ( $inc_b * $coef ) );
	
	return 0;
}

function get_double_match_result($coef, $a, $b, &$sa1, &$sa2, &$sb1, &$sb2) {
	
	$coef = doubleval ( $coef );
	$a = intval ( $a );
	$b = intval ( $b );
	$sa1 = intval ( $sa1 );
	$sa2 = intval ( $sa2 );
	$sb1 = intval ( $sb1 );
	$sb2 = intval ( $sb2 );
	
	// dump ( $coef );
	// dump ( $a );
	// dump ( $b );
	// dump ( $sa1 );
	// dump ( $sa2 );
	// dump ( $sb1 );
	// dump ( $sb2 );
	
	if ($a == $b) {
		return - 1;
	}
	if ($a < $b) {
		return get_double_match_result ( $coef, $b, $a, $sb1, $sb2, $sa1, $sa2 );
	}
	if ($a == $b + 1 || $a < 11 || ($a > 11 && $a != $b + 2)) {
		return - 1;
	}
	
	$pa = 10;
	$pb = $b > 10 ? 10 : $b;
	
	$inc_a1 = ($sb1 + $sb2) * 0.05 * $sa1 / ($sa1 + $sa2) + $pa * ($sb1 + $sb2) * 0.01 * $sa1 / ($sa1 + $sa2) - $pb * $sa1 * 0.01 + 30;
	$inc_a2 = ($sb1 + $sb2) * 0.05 * $sa2 / ($sa1 + $sa2) + $pa * ($sb1 + $sb2) * 0.01 * $sa2 / ($sa1 + $sa2) - $pb * $sa2 * 0.01 + 30;
	$inc_b1 = - $sb1 * 0.05 + $pb * ($sa1 + $sa2) * 0.01 * $sb1 / ($sb1 + $sb2) - $pa * $sb1 * 0.01 + 10;
	$inc_b2 = - $sb2 * 0.05 + $pb * ($sa1 + $sa2) * 0.01 * $sb2 / ($sb1 + $sb2) - $pa * $sb2 * 0.01 + 10;
	
	$sa1 += intval ( round ( $inc_a1 * $coef ) );
	$sa2 += intval ( round ( $inc_a2 * $coef ) );
	$sb1 += intval ( round ( $inc_b1 * $coef ) );
	$sb2 += intval ( round ( $inc_b2 * $coef ) );
	
	return 0;
}

function get_color($gender) {
	if ($gender == 'male') {
		return '#4A4AFF';
	} else if ($gender == 'female') {
		//return '#FF359A';
		return '#FF00FF';
	} else {
		return 'black';
	}
}